package com.example.user.customviewdemo.recyclerview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.user.customviewdemo.R;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RecyclerViewActivity extends AppCompatActivity {

    @Bind(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        ButterKnife.bind(this);

        //添加布局管理
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //添加适配器
        RecyclerAdapter adapter = new RecyclerAdapter(this);
        adapter.setData(getData());
        mRecyclerView.setAdapter(adapter);
        //添加item间隔修饰
       mRecyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.HORIZONTAL,2, Color.GRAY));
    }

    /**
     * 获取数据源
     * @return
     */
    private ArrayList<HashMap<String,Object>> getData(){
        ArrayList<HashMap<String,Object>> data = new ArrayList<>();
        HashMap<String,Object> map;
        for (int i = 0;i<100;i++){
            map = new HashMap<>();
            if(i % 3 == 0){
                map.put("img",R.mipmap.dog);
            }else if(i%2 == 0){
                map.put("img",R.mipmap.flower);
            }else if(i % 5 == 0){
                map.put("img",R.mipmap.image_red);
            }else {
                map.put("img",R.mipmap.ic_launcher);
            }
            map.put("text","desire"+i);
            data.add(map);
        }
        return data;
    }

    /**
     * item间的修饰
     */
    private class MyItemDecoration extends RecyclerView.ItemDecoration{
        private int space;
        public MyItemDecoration(int space){
            this.space = space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view) % 3 == 0){
                outRect.top=space + 70;
            }
        }
    }


    /**
     * RecyclerView的适配器
     */
    public static class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

        private LayoutInflater inflater;
        private ArrayList<HashMap<String, Object>> data;

        public RecyclerAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public void setData(ArrayList<HashMap<String, Object>> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.item_recyclerview_layout, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            HashMap<String,Object> map = data.get(position);
            holder.imageView.setImageResource((Integer) map.get("img"));
            holder.textView.setText((CharSequence) map.get("text"));
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        static class MyViewHolder extends RecyclerView.ViewHolder{
            @Bind(R.id.item_recycler_imageview)
            ImageView imageView;
            @Bind(R.id.item_recycler_textview)
            TextView textView;

            MyViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
